package com.example.ecommerce.controller;

import com.example.ecommerce.dto.ProductCategoryDTO;
import com.example.ecommerce.service.ProductCategoryService;
import com.example.ecommerce.util.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/productCategories")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService categoryService;

    @GetMapping("/{id}")
    public ResponseEntity<ProductCategoryDTO> getCategory(@PathVariable Long id) {
        return ResponseEntity.ok(categoryService.getCategoryById(id));
    }

    @GetMapping("/tree")
    public ActionResult<List<ProductCategoryDTO>> getCategoryTree() {
        return ActionResult.success(categoryService.getCategoryTree());
    }

    @PostMapping
    public ResponseEntity<ProductCategoryDTO> createCategory(@RequestBody ProductCategoryDTO categoryDTO) {
        return ResponseEntity.ok(categoryService.createCategory(categoryDTO));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ProductCategoryDTO> updateCategory(
            @PathVariable Long id,
            @RequestBody ProductCategoryDTO categoryDTO) {
        return ResponseEntity.ok(categoryService.updateCategory(id, categoryDTO));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return ResponseEntity.ok().build();
    }
} 